//
//  ProjectView.swift
//  JKTools
//
//  Created by 姜奎 on 2024/10/14.
//

import Cocoa

class ProjectView: NSView {
    var cornerRadius: CGFloat = 8.0
    var backgroundColor = NSColor.clear
    lazy var projectNameLab = {
        let projectNameLab = NSImageLabel()
        projectNameLab.image = NSImage(named: "Image")
        self.addSubview(projectNameLab, positioned: .below, relativeTo: nil)
        let lead = projectNameLab.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 10)
//        let trail = projectNameLab.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: -10)
        let top = projectNameLab.topAnchor.constraint(equalTo: self.topAnchor, constant: 10)
        let bottom = projectNameLab.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10)
        projectNameLab.setContentCompressionResistancePriority(.required, for: .horizontal)
        self.addConstraints([lead,top,bottom])
        return projectNameLab
    }()

    lazy var currentBranchLab = {
        let currentBranchLab = NSImageLabel()
        currentBranchLab.image = NSImage(named: "Git")
        self.addSubview(currentBranchLab, positioned: .below, relativeTo: nil)
        let lead = currentBranchLab.leadingAnchor.constraint(equalTo: self.projectNameLab.trailingAnchor, constant: 0)
//        let trail = currentBranchLab.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: -10)
        let top = currentBranchLab.topAnchor.constraint(equalTo: self.topAnchor, constant: 10)
        let bottom = currentBranchLab.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10)
        currentBranchLab.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        self.addConstraints([lead,top,bottom])
        return currentBranchLab
    }()

    lazy var statusLab = {
        let statusLab = NSBorderLabel()
        self.addSubview(statusLab, positioned: .above, relativeTo: nil)
        let lead = statusLab.leadingAnchor.constraint(greaterThanOrEqualTo: currentBranchLab.trailingAnchor, constant: 0)
        let trail = statusLab.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10)
        let top = statusLab.topAnchor.constraint(equalTo: self.topAnchor, constant: 10)
        let bottom = statusLab.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10)
        statusLab.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
        self.addConstraints([lead,trail,top,bottom])
        return statusLab
    }()

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        // 保存当前图形上下文状态

        guard let context = NSGraphicsContext.current?.cgContext else { return }

        context.saveGState()

        context.setFillColor(backgroundColor.cgColor)

        // 绘制边框
        let path = NSBezierPath(roundedRect: bounds, xRadius: cornerRadius, yRadius: cornerRadius)

        path.addClip()

        path.fill()

        // 恢复图形上下文状态
        context.restoreGState()
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    private func setup() {
        clipsToBounds = true
        translatesAutoresizingMaskIntoConstraints = false
    }
}
